/*
 * @Author: cooper
 * @Date: 2025-03-30 21:35:19
 * @Wechat: cmd_develop
 * @Description:
 */
#ifndef BSP_JSON
#define BSP_JSON

#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

#define json_DATA_SIZE 1024
#define json_work_queue_array_num 10

extern QueueHandle_t json_work_queue;

void JSON_USART_INIT(void);
void JSON_TASK(void);
void JSON_USART_DMA_TX(u16 size);
void json_cmd_queue_send(u8 code[3]);

void JSON_client_init(void);
void JSON_client_bake(u8 code[json_work_queue_array_num]);
void JSON_client_bake_times(u8 code[json_work_queue_array_num]);
void JSON_client_sale(u8 code[json_work_queue_array_num]);
void JSON_client_check(u8 code[json_work_queue_array_num]);
void JSON_client_control(u8 code);
void JSON_client_power(u8 device, u8 led);
void JSON_client_abandon(u8 code[json_work_queue_array_num]);
void JSON_client_finish(void);
void JSON_client_network(u8 code);
void JSON_client_icebox(u8 code);
void JSON_client_status(void);
void JSON_client_move(u8 code[json_work_queue_array_num]);
void JSON_client_error(u8 code[json_work_queue_array_num]);
void JSON_client_test(void);

typedef struct
{
    u8 rx_data[json_DATA_SIZE]; /*接收数据缓冲区*/
    u16 rx_total;               /*接收数据大小*/
    u8 tx_data[json_DATA_SIZE]; /*发送数据缓冲区*/
    u16 tx_total;               /*发送总数据大小*/
} json_buffer;

extern json_buffer json_Buffer;

#define RX_JSON(x) json_Buffer.rx_data[x]
#define RX_JSON_TOTAL json_Buffer.rx_total
#define TX_JSON(x) json_Buffer.tx_data[x]
#define TX_JSON_TOTAL json_Buffer.tx_total

#define RX_JSON_BUFCLEAR()                      \
    do                                          \
    {                                           \
        for (u16 i = 0; i < RX_JSON_TOTAL; i++) \
        {                                       \
            RX_JSON(i) = 0;                     \
        }                                       \
        RX_JSON_TOTAL = 0;                      \
    } while (0)
#define TX_JSON_BUFCLEAR()                      \
    do                                          \
    {                                           \
        for (u16 i = 0; i < TX_JSON_TOTAL; i++) \
        {                                       \
            TX_JSON(i) = 0;                     \
        }                                       \
        TX_JSON_TOTAL = 0;                      \
    } while (0)

typedef enum
{
    JSON_WAIT,
    SEND_INIT_CODE,
    SEND_BAKE_CODE,
    SEND_BAKE_TIMES_CODE,
    SEND_SALE_CODE,
    SEND_CHECK_CODE,
    SEND_CONTROL_CODE,
    SEND_POWER_CODE,
    SEND_ABANDON_CODE,
    SEND_MOVE_CODE,
    SEND_FINISH_CODE,
    SEND_ICE_CODE,
    SEND_ERROR_CODE,
    SEND_TEST_CODE,
    SERVER_RX_CODE
} JSON_CODE;

#endif
